package com.wandian.admin;

import java.io.IOException;
import java.net.UnknownHostException;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;

/**
 * @author long
 * @Create 2012-11-13 下午4:15:47
 * @QQ 108232706
 * @version 1.0
 * @description AdminCmdRequestor
 */

public class AdminCmdRequestor extends Thread
{
	private static AdminCmdRequestor adminCmdRequestor;
	private final Socket socket;
	private final BufferedReader reader;
	private final BufferedWriter writer;

	public AdminCmdRequestor(String ip, int port)
			throws UnknownHostException, IOException
	{
		this.socket = new Socket(ip, port);
		this.reader = new BufferedReader(new InputStreamReader(this.socket.getInputStream(), "UTF-8"));
		this.writer = new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream(), "UTF-8"));
	}

	@Override
	public void run()
	{
		if ((this.reader == null) || (this.writer == null))
			return;
		try
		{
			String line;
			while ((line = this.reader.readLine()) != null);
			{
				System.out.println(line);
			}
		} catch (IOException e)
		{
			close();
		}
	}

	public void send(String line) {
		try {
			this.writer.write(line);
			this.writer.newLine();
			this.writer.flush();
		} catch (IOException e) {
			close();
		}
	}

	public void close() {
		try {
			if (!this.socket.isClosed()) {
				this.socket.close();
			}
			adminCmdRequestor = null;
			System.exit(0);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}

	public static void main(String[] args)
	{
		System.out.println("welcome admin... type quit or exit to exit");

		if (args.length == 0) {
			String ip = "10.10.3.55";
			int port = 6100;
			adminCmdRequestor = connect(ip, port);
		} else {
			printConnectHelp();
		}
		try
		{
			BufferedReader in = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
			String line;
			while ((line = in.readLine()) != null)
			{
				if (line.trim().length() != 0)
				{
					String[] params = line.split(" ");
					if (adminCmdRequestor == null) {
						if ((params.length == 3) && (params[0].equals("connect"))) {
							String ip = params[1];
							int port = Integer.valueOf(params[2]).intValue();
							adminCmdRequestor = connect(ip, port);
						} else if ((params[0].equals("exit")) || (params[0].equals("quit"))) {
							System.exit(0);
						} else {
							System.out.println("输入有误!");
							printConnectHelp();
						}
					}
					else if ((params[0].equals("exit")) || (params[0].equals("quit"))) {
						adminCmdRequestor.close();
						System.out.println("服务器连接已经断开");
					} else {
						adminCmdRequestor.send(line);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static AdminCmdRequestor connect(String ip, int port) {
		try {
			AdminCmdRequestor adminCmdRequestor = new AdminCmdRequestor(ip, port);
			adminCmdRequestor.start();
			return adminCmdRequestor;
		} catch (UnknownHostException e) {
			System.out.println("服务器主机地址或者端口错误");
		} catch (IOException e) {
			System.out.println("服务器未启动或者连接失败");
		} catch (Exception e) {
			System.out.println("服务器主机地址或者端口错误");
			printConnectHelp();
		}
		return null;
	}

	private static void printConnectHelp() {
		System.out.println("请输入连接的服务器ip & port");
		System.out.println("connect ip port");
	}
}